'''
Created on 16/08/2010

@author: eh01
'''
from datastore.resource import Resource, ResourceDict
from datastore.spaces import Spaces
from services.makohttptemplate import MakoHTTPTemplate
from spear.a2p_service import A2PService
from xml.dom import minidom
import os
import unittest
from adaptor.services.application_list_service import ApplicationListService
from datastore.uri import Uri


class ApplicationListServiceTest(unittest.TestCase):

    SITE_TEMPLATE_FILE_PATH = '../../../sites/adaptor/templates/adaptor-template.xml'
    SITE_TEMPLATE_FILE_PATH_WHEN_RUNNING_FROM_ANT = '../sites/adaptor/templates/adaptor-template.xml'
    TEMPLATE_DIRECTORY = '../../../sites/adaptor/templates'
    TEMPLATE_DIRECTORY_WHEN_RUNNING_FROM_ANT = '../sites/adaptor/templates'

    def setUp(self):
        pass

    def tearDown(self):
        pass
    
    def stub_get_application_list(self):
        apps_dict = {}
        app_list_rs = Resource(type="temp")
        app_list_rs.apps_dict_dict = apps_dict
        return app_list_rs

    def stub_get_empty_application_list(self):
        return None    
    
    def test_template(self):
        site_template_res = Resource(type="temp")
        if os.path.isfile(ApplicationListServiceTest.SITE_TEMPLATE_FILE_PATH):
            site_template_res.load_from_file(ApplicationListServiceTest.SITE_TEMPLATE_FILE_PATH)
            template_dir =  ApplicationListServiceTest.TEMPLATE_DIRECTORY
        else:
            site_template_res.load_from_file(ApplicationListServiceTest.SITE_TEMPLATE_FILE_PATH_WHEN_RUNNING_FROM_ANT)
            template_dir =  ApplicationListServiceTest.TEMPLATE_DIRECTORY_WHEN_RUNNING_FROM_ANT        
        spaces = Spaces()
        template_res = Resource(type="temp")
        template_res.load_from_file('/'.join([template_dir, 'application-list.xml']))
        res = Resource(type="term:eplanning/adaptor/application-list/data")
        res.applications = {'first_app':{'spear_ref':'S000001E',
                                   'state':A2PService.APPSTATE_INSYNCWITHSPEAR,
                                   'Property_Details':'123 Fake St',
                                   'Applicant_Contact':'TestApplicantContact',
                                   'Type':'ApplicationType',
                                   'Council_Ref':'Ref123',
                                   'Status':'ApplicationStatus',
                                   },
                            'second_app':{'spear_ref':'S000002E',
                                   'state':A2PService.APPSTATE_SUMMARYINFOONLY,
                                   'Property_Details':'456 Fake St',
                                   'Applicant_Contact':'TestApplicantContact',
                                   'Type':'ApplicationType',
                                   'Council_Ref':'Ref456',
                                   'Status':'ApplicationStatus',
                                   },
                            'third_app':{'spear_ref':'S000003E',
                                   'state':A2PService.APPSTATE_UPDATINGFROMSPEAR,
                                   'Property_Details':'789 Fake St',
                                   'Applicant_Contact':'TestApplicantContact',
                                   'Type':'ApplicationType',
                                   'Council_Ref':'Ref789',
                                   'Status':'ApplicationStatus',
                                   }}
        res.h1_header = 'Test Header'
        x = MakoHTTPTemplate(spaces, site_template_res, template_res, res)
        html = raw_xml(x.render().strip())
        
        # Test the first app and the second app renders in the table correctly and that 
        # one is hyperlinked and the other is not
        first_app_html = '<tr><td><a href="/adaptor/application?spearRef=S000001E">123 Fake St</a></td><td>TestApplicantContact</td><td>ApplicationType</td><td>Ref123</td><td>ApplicationStatus</td><td>S000001E</td><td>In Sync with SPEAR</td></tr>'
        second_app_html = '<tr><td>456 Fake St</td><td>TestApplicantContact</td><td>ApplicationType</td><td>Ref456</td><td>ApplicationStatus</td><td>S000002E</td><td>Summary Info Only</td></tr>'
        third_app_html = '<tr><td><a href="/adaptor/application?spearRef=S000003E">789 Fake St</a></td><td>TestApplicantContact</td><td>ApplicationType</td><td>Ref789</td><td>ApplicationStatus</td><td>S000003E</td><td>Fetching from SPEAR</td></tr>'
        self.assertTrue(first_app_html in html)
        self.assertTrue(second_app_html in html)
        self.assertTrue(third_app_html in html)

    def test_cb_start(self):
        spaces = Spaces()
        application_list_service = ApplicationListService(spaces, None, Uri())
        application_list_service.cb_start(None, None, None)
        self.assertEqual([application_list_service.cb_list], spaces.process[Uri('external:adaptor/application-list/')])
        self.assertEqual([application_list_service.cb_list], spaces.process[Uri('external:adaptor/application-list')])  
    
    def test_cb_list_get(self):
        spaces = Spaces()
        application_list_service = ApplicationListService(spaces, None, Uri())
        
        # test non empty list
        application_list_service.get_application_list = self.stub_get_application_list
        res = application_list_service.cb_list(Uri(), Spaces.ACTION_GET, None)
        self.assertEqual(ApplicationListService.SPEAR_CURRENT_APPLICATION_LIST, res.h1_header)
        self.assertNotEqual(None, res.applications)
        
        # test empty list
        application_list_service.get_application_list = self.stub_get_empty_application_list
        res = application_list_service.cb_list(Uri(), Spaces.ACTION_GET, None)
        self.assertEqual(ApplicationListService.SPEAR_CURRENT_APPLICATION_LIST, res.h1_header)
        self.assertEqual(ApplicationListService.UNABLE_TO_FIND_ANY_CURRENT_SPEAR_APPLICATIONS, res.message)

    def test_cb_list_put(self):
        spaces = Spaces()
        application_list_service = ApplicationListService(spaces, None, Uri())
        application_list_service.get_application_list = self.stub_get_application_list
        res_uri = application_list_service.cb_list(Uri(), Spaces.ACTION_PUT, None)
        self.assertEqual(None, res_uri)

    def test_cb_list_update(self):
        spaces = Spaces()
        application_list_service = ApplicationListService(spaces, None, Uri())
        application_list_service.get_application_list = self.stub_get_application_list
        res_uri = application_list_service.cb_list(Uri(), Spaces.ACTION_UPDATE, None)
        self.assertEqual(None, res_uri)
        
    def test_cb_list_exists(self):
        spaces = Spaces()
        application_list_service = ApplicationListService(spaces, None, Uri())
        application_list_service.get_application_list = self.stub_get_application_list
        ret = application_list_service.cb_list(Uri(), Spaces.ACTION_EXISTS, None)
        self.assertTrue(ret)
        
    def test_cb_list_delete(self):
        spaces = Spaces()
        application_list_service = ApplicationListService(spaces, None, Uri())
        application_list_service.get_application_list = self.stub_get_application_list
        ret = application_list_service.cb_list(Uri(), Spaces.ACTION_DELETE, None)
        self.assertEqual(None, ret)
        
    def test_cb_list_remove(self):
        spaces = Spaces()
        application_list_service = ApplicationListService(spaces, None, Uri())
        application_list_service.get_application_list = self.stub_get_application_list
        res = application_list_service.cb_list(Uri(), Spaces.ACTION_REMOVE, None)
        self.assertEqual(None, res)        
    
    def test_cb_list_find(self):
        spaces = Spaces()
        application_list_service = ApplicationListService(spaces, None, Uri())
        application_list_service.get_application_list = self.stub_get_application_list
        lst = application_list_service.cb_list(Uri(), Spaces.ACTION_DELETE, None)
        self.assertEqual(None, lst)
        
def raw_xml(xml_str):
    xml = minidom.parseString(xml_str)
    return u''.join([unicode(line).strip() for line in xml.toprettyxml().splitlines()])

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()